package com.hwl.acl.controller;


import com.hwl.acl.pojo.Permission;
import com.hwl.acl.service.PermissionService;
import com.hwl.commonutils.result.RespBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 权限 前端控制器
 * </p>
 *
 * @author long
 * @since 2021-10-16
 */
@Api(tags = "后台权限管理")
@RestController
@RequestMapping("/admin/acl/permission")
public class PermissionController {

    @Resource
    private PermissionService permissionService;

    @ApiOperation("查询所有菜单")
    @GetMapping("getAllMenu")
    public RespBean getAllMenu() {
        return RespBean.ok().data(permissionService.queryAllMenu());
    }

    @ApiOperation("删除菜单")
    @DeleteMapping("remove/{id}")
    public RespBean remove(@PathVariable String id) {
        if (permissionService.removeChildById(id)) {
            return RespBean.ok().message("删除菜单成功！");
        }
        return RespBean.error().message("删除菜单失败！");
    }

    @ApiOperation("给角色分配权限")
    @PostMapping("doAssign")
    public RespBean doAssign(String roleId, String[] permissionId) {
        if (permissionService.assignRolePermissions(roleId, permissionId)) {
            return RespBean.ok().message("权限分配成功！");
        }
        return RespBean.error().message("权限分配失败！");
    }

    @ApiOperation("根据角色获取菜单")
    @GetMapping("toAssign/{roleId}")
    public RespBean toAssign(@PathVariable String roleId) {
        return RespBean.ok().data(permissionService.selectAllMenu(roleId));
    }

    @ApiOperation("新增菜单")
    @PostMapping("save")
    public RespBean save(@RequestBody Permission permission) {
        if (permissionService.save(permission)) {
            return RespBean.ok().message("新增菜单成功！");
        }
        return RespBean.error().message("添加菜单失败！");
    }

    @ApiOperation("更新菜单")
    @PutMapping("update")
    public RespBean updateById(@RequestBody Permission permission) {
        if (permissionService.updateById(permission)) {
            return RespBean.ok().message("更新菜单成功！");
        }
        return RespBean.error().message("更新菜单失败！");
    }

}

